$(function () {
    init_id_name();
    init_validates();
})

function init_id_name(){
    var name = $("#sale-tag-nameid").val();
    var uuid =  $("#sale-tag-upid").val();
    $("#tagUuidss").val(uuid);
    $("#updateTagName").val(name);
}

/*开启表单验证*/
function init_validates(){
    var groupid = $("#sale-tag-selectid").val();
    $("#tagGroupId").val(groupid);
    var $saleTagForm = $("#sale-updateTag-form");
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw color-red'></i> ";
    var validator = $saleTagForm.validate({
        rules:{
            tagName:{
                required:true,
                minlength:1,
                maxlength:6,
                remote:{
                    url:ctx+"saleConfig/checkTagName",
                    type: "get",
                    dataType: "json",
                    data: {
                        tagName: function (){
                            return $("#tagName").val().trim();
                        },
                        tagGroupId: function (){
                            return $("#tagGroupId").val().trim();
                        }
                    }
                }
            }
        },
        messages:{
            tagName:{
                required: icon + "请输入标签名称",
                minlength: icon + "标签名称长度1到6个字符",
                maxlength: icon + "标签名称长度1到6个字符",
                remote: icon + "标签名称已经存在"
            }
        }
    });
}

function updateTagNames() {
    var $saleTagForm = $("#sale-updateTag-form");
    var validator = $saleTagForm.validate();
    var flag = validator.form();
    if(flag){
        $.post(ctx+"saleConfig/updateTagName",$("#sale-updateTag-form").serialize(),function (r) {
            if (r.code === 0) {
                $("#sale-modal").modal('hide');
                afterAdd();
                $JuXue.n_success(r.msg);
            } else {
                $JuXue.n_danger(r.msg);
            }
        });
    }
}